package com.zhang.controller;

import com.zhang.pojo.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.MultipartFilter;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @program: big_event
 * @description: TODO
 * @author: Mr.Zhang
 * @create: 2024-04-23 11:36
 **/
@RestController
public class FileUploadController {

    /**
     * 上传文件
     * @param file
     * @return
     * @throws IOException
     */
    public Result<String> upload(MultipartFile file) throws IOException {
        // 获取上传文件的原始文件名
        String originalFilename = file.getOriginalFilename();
        //保证文件名唯一，从而防止文件覆盖
        String fleName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));;
        // 将文件保存到指定路径
        file.transferTo(new File("D:\\Desktop\\springboot\\files\\"+fleName));
        // 返回上传成功的消息
        return Result.success("url访问地址。。。");
    }
}
